import dayjs from 'dayjs';
import { patientindexsearchcity, customerwechattagslist } from '@/api/hqapi';
import { patientindextagsconditionlistv2 } from '@/api/hqapi/tags';
import searchTree from '@riophae/vue-treeselect'
/**
 * 通用混合
 * */
export default {
    components: {searchTree},
    data() {
        return {
            area_code: [],
            type: 2,
            optionscity: [],
            city_id: '',
            loadingcity: false,
            loading: false,
            list: [],
            total: 0,
            belongData:[],
            menberList:[],  
            belong_member_id:[],      
            params: {
                area_code: '',
                dialog_mode_id: '',
                created_stime: new Date(),
                created_etime: new Date(),
                appointment_stime: new Date(),
                appointment_etime: new Date(),
                revisiting_stime: new Date(),
                revisiting_etime: new Date(),
                operate_stime: new Date(),
                operate_etime: new Date(),
                keywords: '',
                channel_id: [],
                department_id:'',
                belong_member_id: '',
                patient_status_id: '',
                tags_ids: '',
                project_tags_ids: '',
                is_attention: '',
                sort_appointment_time: 0,
                sort_callback_count: 0,
                sort_last_callback_time: 0,
                sort_created_at: 0,
                page: 1,
                pageSize: 10,
                have_mobile: 0,
                have_wechat: 0,
                have_repeat: 0,
                body_parts_id: '',
                have_contact_type: '',

            },
            filteredValue: [],
            timer: null,
            Checkboxval: [],
            initialbool: true
        };
    },
    mounted() {
        let Projectid = localStorage.getItem('currentProject');
        let Projectcity = JSON.parse(localStorage.getItem('project'));
        for(let item of Projectcity) {
            if(+Projectid === +item.project_id && item.city_name !== '') {
                this.optionscity = [{
                    key: item.default_city,
                    value: item.default_city,
                    label: item.city_name
                } ];
            }
        }
    },
    methods: {
        
        initial() {
            if(this.initialbool) {
                this.params.page = 1;
                this.askData();
                this.initialbool = false;
            }
        },
        cityvalue(city) {
            this.area_code = city;
            this.params.area_code = city.join(',');
            this.params.page = 1;
            this.askData();
        },
        askDatas(e) {
            this.params = e;
            this.askData();
        },
        askDataing() {
            this.params.page = 1;
            this.askData();
        },
        inputaskData() {
            this.params.page = 1;
            this.params.have_mobile = 0;
            this.params.have_wechat = 0;
            this.params.have_repeat = 0;
            this.params.is_attention = 0;
            this.params.city_id = '';
            this.type = undefined;
            this.searchData.project.forEach(item => {
                if(item.id === 4) {
                    item.value = [];
                }else if(item.id === 6) {
                    item.value = '';
                }else if(item.id === 7) {
                    item.value = [];
                }else if(item.id === 8) {
                    item.value = '';
                }else if(item.id === 9) {
                    item.value = '';
                }else if(item.id === 10) {
                    item.value = [];
                    item.childList = [];
                }else if(item.id === 12) {
                    item.value = [];
                }else if(item.id === 20) {
                    item.value = [];
                }else if(item.id === 21) {
                    item.value = '';
                }
            });
            this.askData();
        },
        gettags(){
            patientindextagsconditionlistv2().then(res => {
                this.searchData.project.forEach(item => {
                    if(item.id == 30) {
                        item.value = '';
                        item.childList = res.self_tags;
                    }else if(item.id == 31) {
                        item.value = '';
                        item.childList = res.project_tags;
                    }
                });
            });

        },
        getparams() {
            this.searchData.project.forEach(item => {
                if(item.id === 4) {
                    this.params.created_stime = item.value[0] ? item.value[0] : '';
                    this.params.created_etime = item.value[1] ? item.value[1] : '';
                    this.params.appointment_stime = item.value[0] ? item.value[0] : '';
                    this.params.appointment_etime = item.value[1] ? item.value[1] : '';
                    this.params.revisiting_stime = item.value[0] ? item.value[0] : '';
                    this.params.revisiting_etime = item.value[1] ? item.value[1] : '';
                    this.params.operate_stime = item.value[0] ? item.value[0] : '';
                    this.params.operate_etime = item.value[1] ? item.value[1] : '';
                }else if(item.id === 5) {
                    this.params.keywords = item.value;
                }else if(item.id === 6) {
                    this.params.patient_status_id = item.value;
                }else if(item.id === 8) {
                    this.params.patient_diseases_id = item.value;
                }else if(item.id === 11) {
                    this.params.belong_member_id = this.belong_member_id.length>0?this.belong_member_id.join(','):'';
                }else if(item.id === 9) {
                    if(item.value.length > 0) this.params.channel_id = item.value.join(',');
                    else this.params.channel_id = '';
                }else if(item.id === 12) {
                    this.params.dialog_mode_id = item.value;
                }else if(item.id === 20) {
                    this.params.body_parts_id = item.value.join(',');
                }else if(item.id === 21) {
                    this.params.have_contact_type = item.value;
                }else if(item.id == 30) {
                    let list = [];
                    for(let it of item.value) {
                        list.push(it.split(',')[0]);
                    }
                    this.params.tags_ids = list.join(',');
                }else if(item.id == 31) {
                    let list = [];
                    for(let it of item.value) {
                        list.push(it.split(',')[0]);
                    }
                    this.params.project_tags_ids = list.join(',');
                }
            });
        },
        getDatas() {
            for(let item of this.columns) {
                if(item.title === '未访天数') {
                    item.filteredValue = [];
                    this.params.not_callback_start = '';
                    this.params.not_callback_end = '';
                }
            }
            this.type = undefined;
            this.params.page = 1;
            this.askData();
        },
        askDatachangess() {
            this.params.page = 1;
            this.searchData.project.forEach(item => {
                if(item.id === 10) {
                    this.params.tags_ids = item.value.join(',');
                }
            });
            this.askData();
        },
        checkbox(e) {
            this.params.page = 1;
            // for(let item of this.columns) {
            //     if(item.title === '未访天数') {
            //         item.filteredValue = [];
            //         this.params.not_callback_start = '';
            //         this.params.not_callback_end = '';
            //     }
            // }
            // this.searchData.project.forEach(item => {
            //     if(item.id === 4) {
            //         item.value = ['', ''];
            //     }
            // });
            this.params.is_attention = 0;
            this.params.have_mobile = 0;
            this.params.have_wechat = 0;
            this.params.have_repeat = 0;
            this.params.is_share = 0;
            for(let item of e) {
                if(item == 'attention') this.params.is_attention = 1;
                else if(item == 'mobile') this.params.have_mobile = 1;
                else if(item == 'wechat') this.params.have_wechat = 1;
                else if(item == 'repeat') this.params.have_repeat = 1;
                else if(item == 'is_share') this.params.is_share = 1;
            }
            this.askData();
        },
        changetype(type) {
            for(let item of this.columns) {
                if(item.title === '未访天数') {
                    item.filteredValue = [];
                    this.params.not_callback_start = '';
                    this.params.not_callback_end = '';
                }
            }
            let week = dayjs().day();
            let weekday = dayjs().add(7 - week, 'day').format('YYYY-MM-DD');
            let today = dayjs().format('YYYY-MM-DD');
            let yesday = dayjs().subtract(1, 'day').format('YYYY-MM-DD');
            let tomday = dayjs().add(1, 'day').format('YYYY-MM-DD');
            let sday = dayjs(`${dayjs().format('YYYY-MM')}-1`).format('YYYY-MM-DD');
            let eday = dayjs(`${dayjs().year()}-${dayjs().month() + 2}`).subtract(1, 'day').format('YYYY-MM-DD');
            this.searchData.project.forEach(item => {
                if(item.id === 4) {
                    if(type === 1) item.value = [yesday, yesday];
                    if(type === 2) item.value = [today, today];
                    if(type === 3) item.value = [tomday, tomday];
                    if(type === 4) item.value = [today, weekday];
                    if(type === 5) item.value = [sday, eday];
                }
            });
            this.params.page = 1;
            this.askData();
        },
        remoteMethod(query) {
            clearTimeout(this.timer);
            if(query !== '') {
                let reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
                if(!reg.test(query)) {
                    return;
                }
                this.loadingcity = true;
                return new Promise(resolve => {
                    this.timer = setTimeout(() => {
                        patientindexsearchcity({
                            keywords: query
                        }).then(res => {
                            this.query = '';
                            if(res.list && res.list.length > 0) {
                                let list = [];
                                for(let item of res.list) {
                                    list.push({
                                        value: item.value,
                                        label: item.label
                                    });
                                }
                                this.optionscity = list;
                            }
                            resolve(res.data);
                        });
                        this.loadingcity = false;
                    }, 200);
                });
            }
        },
        
        condition(res) {
            this.searchData.project.forEach(item => {
                if(item.id === 6) {
                    if(res.status_list) item.childList = res.status_list;
                }else if(item.id === 7) {
                    if(res.member_list) item.childList = res.member_list;
                    this.belongData=res.member_list
                    this.menberList=[]
                    res.member_list.forEach(item=>{
                        if(item&&item.children.length>0){
                            this.menberList=[...this.menberList,...item.children]
                        }
                    })
                }else if(item.id === 8) {
                    if(res.diseases) item.childList = res.diseases;
                }else if(item.id === 9) {
                    if(res.source_channel) item.childList = res.source_channel;
                }else if(item.id === 10) {
                    if(res.tags_list) item.childList = res.tags_list;
                }else if(item.id === 12) {
                    if(res.talk_way) item.childList = res.talk_way;
                }else if(item.id === 20) {
                    if(res.talk_way) item.childList = res.body_parts;
                }
            });
        },
        changeseels(){
            this.askData();
        },
        changetag(member_id = '') {
            customerwechattagslist({ member_id }).then(res => {
                this.searchData.project.forEach(item => {
                    if(item.id === 10) {
                        item.value = [];
                        item.childList = res.list;
                    }
                });
                this.params.page = 1;
                this.askData();
            });
        },
        changeDept(e){
            this.params.department_id=e
            this.belong_member_id=[]
            this.menberList=[]
            this.belongData.forEach(item=>{
                if(item.value==e){
                    this.menberList=item.children
                }
            })
            if(!e){
                this.belongData.forEach(item=>{
                    if(item&&item.children.length>0){
                        this.menberList=[...this.menberList,...item.children]
                    }
                })
            }
            this.askDataing()
        },
        
          
    }
};
